package com.caocaog.market.controller;

import com.caocaog.market.entity.Member;
import com.caocaog.market.entity.TiktokEncryptedParam;
import com.caocaog.market.service.TiktokService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.controller
 * @ClassName TiktokController.java
 * @createTime 2022年04月28日 19:15:00
 * @Description TODO
 */
@Slf4j
@RestController
@Api(value = "tiktok（抖音）")
@RequestMapping("/tiktok")
public class TiktokController {
    @Autowired
    private TiktokService tiktokService;
    /**
     * 获取access_token
     */
    @ApiOperation(value = "获取access_token")
    @GetMapping("/accessToken")
    public Result accessToken(){
        log.info("获取access_token--------");
        return tiktokService.getAccessToken();
    }

    /**
     * 获取openid和access_key,并将获取到的信息存入redis,以便用于解密用户信息
     * @param code
     * @return
     */
    @ApiOperation(value = "根据code值获取openid和access_key等信息")
    @GetMapping("/code2Session")
    public Result code2Session(@RequestParam("code") @NotEmpty(message = "code不能为空") String code) {
        log.info("根据code值获取openid和access_key等信息");
        return tiktokService.code2Session(code);
    }

    /**
     * 前端调用该接口用于解密获取到的当前抖音或头条的用户信息
     * @param param
     * @return
     */
    @ApiOperation(value = "解密抖音或头条用户信息")
    @PostMapping("/decrypt/userInfo")
    public Result decryptPhone(@Validated @RequestBody TiktokEncryptedParam param) {
        log.info("解密抖音或头条用户信息");
        return tiktokService.decryptUserInfo(param);
    }

    /**
     * 登录授权方法
     *
     * @param member 登录信息R     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录授权方法", response = Member.class)
    public Result login(@RequestBody Member member) {
        return Result.OK(tiktokService.login(member));
    }



}
